package com.lineng.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.lineng.dao.DownSourceMapper;
import com.lineng.pojo.DownSource;
import com.lineng.service.DownSourceService;
import com.lineng.util.VerificationUtil;

@Service
public class DownSourceServiceImpl implements DownSourceService {
	@Autowired
	DownSourceMapper downsourceMapper;

	@Autowired
	private HttpServletRequest request;

	public List<DownSource> getDownSource() {

		List<DownSource> downSources = downsourceMapper.selectByExample(null);
		if (VerificationUtil.verification(request) == 0) {
			for (int i = 0; i < downSources.size(); i++) {
				if (downSources.get(i).getReader() != null && downSources.get(i).getReader() != 0) {
					int result = downSources.get(i).getReader() + 1;
					downsourceMapper.updateDownSourceReader(result);
				} else {
					downsourceMapper.updateDownSourceReader(1);
				}
			}
		}
		return downSources;
	}

	public int insertDownSource(DownSource downsource, MultipartFile file, String username) {
		int num = 0;
		if (VerificationUtil.verification(request) == 1) {
			// 获取当前时间
			Date time = new Date();
			// 获得项目的路径
			ServletContext sc = request.getSession().getServletContext();
			// 上传位置
			String path = sc.getRealPath("/") + downsource.getPath() + "/" + time.getTime()
					+ file.getOriginalFilename();
			// 设定文件保存的目录
			String path1 = downsource.getPath() + "/" + time.getTime() + file.getOriginalFilename();
			if (file != null) {
				try {
					saveFile(file, path);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			downsource.setPath(path1);
			downsource.setDate(time);
			downsource.setPeople(username);
			if (downsource != null) {
				num = downsourceMapper.insertSelective(downsource);
			}
			return num;
		}
		return num;

	}

	public int deleteDownSource(Integer[] idList) {
		if (VerificationUtil.verification(request) == 1) {
			for (int i = 0; i < idList.length; i++) {
				DownSource downsource = downsourceMapper.selectByPrimaryKey(idList[i]);
				System.out.println(idList[i]);
				File file = new File(request.getSession().getServletContext().getRealPath("/") + downsource.getPath());
				if (file.length() > 0) {
					file.delete();
				}
			}
			return downsourceMapper.deleteById(idList);
		}
		return 0;

	}

	private boolean saveFile(MultipartFile file, String path) throws Exception {
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				File filepath = new File(path);
				if (!filepath.exists())
					filepath.mkdirs();
				// 文件保存路径
				String Url = path;
				// 转存文件
				file.transferTo(new File(Url));
				// throw new Exception();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			}
		}
		return false;
	}

}
